# Distributed under the terms of the GNU General Public License v2

EAPI="7"

inherit autotools flag-o-matic perl-module toolchain-funcs

DESCRIPTION="Zinnia - Online hand recognition system with machine learning"
HOMEPAGE="https://taku910.github.io/zinnia/ https://github.com/taku910/zinnia https://sourceforge.net/projects/zinnia/"
SRC_URI="https://github.com/taku910/zinnia/archive/581faa8f6f15e4a7b21964be3a5ec36265c80e5b.tar.gz -> zinnia-20160901.tar.gz"

LICENSE="BSD"
SLOT="0"
KEYWORDS="*"
IUSE="perl static-libs"

PATCHES=(
	"${FILESDIR}"/${PN}-flags.patch
	"${FILESDIR}"/${PN}-perl.patch
)

HTML_DOCS=( doc/{index{,-ja}.html,${PN}.css} )

src_unpack() {
	unpack ${A}
	mv "${WORKDIR}"/zinnia-* "${S}"
	S="${S}/zinnia"
}

src_prepare() {
	default
	sed -i "s/AM_CONFIG_HEADER/AC_CONFIG_HEADERS/" configure.in || die

	mv configure.{in,ac} || die
	eautoreconf
}

src_configure() {
	econf $(use_enable static-libs static)
}

src_compile() {
	default

	if use perl; then
			cd perl >/dev/null || die
			# We need to run this here as otherwise it won't pick up the
			# just-built -lzinnia and cause the extension to have
			# undefined symbols.
			perl-module_src_configure

			append-cppflags "-I${S}"
			append-ldflags "-L${S}/.libs"

			emake \
				CC="$(tc-getCXX)" \
				LD="$(tc-getCXX)" \
				OPTIMIZE="${CPPFLAGS} ${CXXFLAGS}" \
				LDDLFLAGS="-shared" \
				OTHERLDFLAGS="${LDFLAGS}"
			cd - >/dev/null || die
	fi
}

src_install() {
	default
	einstalldocs
	find "${D}" -name "*.la" -delete || die

	if use perl; then
			cd perl >/dev/null || die
			perl-module_src_install
			cd - >/dev/null || die
	fi
}